package com.emm.yixun.mobile.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.model.NewClassKehuModel;
import com.eroad.product.tools.CLEditText;
import com.eroad.product.tools.PickerView;
import com.eroad.product.tools.PickerView.onSelectListener;

public class NewFromAdapter extends BaseAdapter {

	Activity context;
	ArrayList<NewClassKehuModel> list;

	private Integer index = -1;
	private Integer index2 = -1;

	int type = 0;

	public NewFromAdapter(Activity context, ArrayList<NewClassKehuModel> list) {
		this.list = list;
		this.context = context;
	}

	public void SetDate(ArrayList<NewClassKehuModel> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void SetType(int type) {
		this.type = type;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		return list == null ? null : list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHoudler_for v;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.kehu_item, null);
			v = new ViewHoudler_for();
			v.delete = (ImageView) convertView.findViewById(R.id.delete);
			v.name = (CLEditText) convertView.findViewById(R.id.name);
			v.type = (CLEditText) convertView.findViewById(R.id.type);
			v.code_type = (CLEditText) convertView.findViewById(R.id.code_type);
			v.title_name = (TextView) convertView.findViewById(R.id.title_name);
			v.icon_right_arrow = (ImageView) convertView.findViewById(R.id.icon_right_arrow);
			convertView.setTag(v);
		} else {
			v = (ViewHoudler_for) convertView.getTag();
			v.name.clearTextChangedListeners();
			v.type.clearTextChangedListeners();
			v.code_type.clearTextChangedListeners();
		}
		if (type != 0) {
			v.icon_right_arrow.setVisibility(View.GONE);
			v.delete.setVisibility(View.INVISIBLE);
			v.name.setFocusable(false);
			v.name.setFocusableInTouchMode(false);
			v.type.setFocusable(false);
			v.type.setFocusableInTouchMode(false);
			v.type.setEnabled(false);
			v.code_type.setFocusable(false);
			v.code_type.setFocusableInTouchMode(false);
			v.type.setText(EmmApplication.isNull(list.get(position).getType()) ? list.get(position).getType() : " ");// 先校验是否为空
		} else {
			v.icon_right_arrow.setVisibility(View.VISIBLE);
			v.delete.setVisibility(View.VISIBLE);
			v.name.setFocusable(true);
			v.name.setFocusableInTouchMode(true);
			v.type.setFocusable(false);
			v.type.setFocusableInTouchMode(false);
			v.type.setEnabled(true);
			v.code_type.setFocusable(true);
			v.code_type.setFocusableInTouchMode(true);
			v.type.setText(EmmApplication.isNull(list.get(position).getType()) ? list.get(position).getType() : "");// 先校验是否为空
		}

		v.name.setText(list.get(position).getName());
		v.code_type.setText(list.get(position).getCode());

		v.delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				list.remove(position);
				notifyDataSetChanged();
			}
		});
		v.title_name.setText("家庭成员" + list.get(position).getNumber());

		final CLEditText ed_ = v.type;
		v.type.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ArrayList<String> listname = new ArrayList<String>();
				listname.add("身份证");
				listname.add("护照");
				listname.add("营业执照");
				listname.add("户口薄");
				listname.add("军官证");
				listname.add("临时身份证");
				listname.add("港澳台同胞来往通行证");
				listname.add("边民出入境通行证");
				listname.add("外国人永久居留证");
				listname.add("警官证");
				listname.add("士兵证");
				listname.add("其他");
				SetzpPopu(listname, "证件类型", ed_, position);
			}
		});

		v.name.setTag(position);
		v.name.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					index = (Integer) v.getTag();
				}
				return false;
			}
		});
		class MyTextWatcher implements TextWatcher {
			public MyTextWatcher(ViewHoudler_for holder) {
				mHolder = holder;
			}

			private ViewHoudler_for mHolder;

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s != null && !"".equals(s.toString())) {
					int position = (Integer) mHolder.name.getTag();

					list.get(position).setName(s.toString());
				}
			}
		}
		v.name.addTextChangedListener(new MyTextWatcher(v));

		v.code_type.setTag(position);
		v.code_type.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					index2 = (Integer) v.getTag();
				}
				return false;
			}
		});
		class MyTextWatcher2 implements TextWatcher {
			public MyTextWatcher2(ViewHoudler_for holder) {
				mHolder = holder;
			}

			private ViewHoudler_for mHolder;

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s != null && !"".equals(s.toString())) {
					int position = (Integer) mHolder.code_type.getTag();
					list.get(position).setCode(s.toString());
				}
			}
		}
		v.code_type.addTextChangedListener(new MyTextWatcher2(v));

		return convertView;
	}

	public static class ViewHoudler_for {
		ImageView delete;

		CLEditText name;

		CLEditText type;

		CLEditText code_type;

		TextView title_name;

		ImageView icon_right_arrow;

	}

	/* ---公用选择框--- */
	View view;
	PopupWindow popu;
	/** 选中内容 */
	String selectName;
	/** 选中下标 */
	int SelectID;

	/* ---公用选择框--- */

	/** 选项框 */
	public void SetzpPopu(final List<String> listname, String title, final EditText ed, final int p) {
		view = LayoutInflater.from(context).inflate(R.layout.popu_zp, null);
		popu = new PopupWindow(view, android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT,
				android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT);
		popu.setFocusable(true);
		// EmmApplication.SetNumberLog(context, 0.5f);// 改变popupwindow范围外的屏幕亮度
		popu.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
		popu.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		popu.showAtLocation(view, Gravity.BOTTOM, 0, 0);
		popu.showAsDropDown(view, 0, 0);
		popu.setFocusable(true);
		popu.setOutsideTouchable(true);
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					popu.dismiss();
					EmmApplication.setLog(context);
				}
				return false;
			}
		});
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				popu.dismiss();
				EmmApplication.setLog(context);
			}
		});

		PickerView select_pick = (PickerView) view.findViewById(R.id.select_pick);

		selectName = listname.get(0);

		TextView shuerbtn = (TextView) view.findViewById(R.id.shuerbtn);
		RelativeLayout relative_all = (RelativeLayout) view.findViewById(R.id.relative_all);
		ImageView dis_btn = (ImageView) view.findViewById(R.id.dis_btn);
		dis_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popu.dismiss();
				EmmApplication.setLog(context);
			}
		});
		TextView title_select = (TextView) view.findViewById(R.id.title_select);

		title_select.setText(title);

		select_pick.setData(listname);
		select_pick.setOnSelectListener(new onSelectListener() {
			@Override
			public void onSelect(String text, int id) {
				selectName = text;
				SelectID = id;
			}
		});
		select_pick.setSelected(0);
		shuerbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 确定
				popu.dismiss();
				EmmApplication.setLog(context);
				// EmmApplication.T("选择:" + selectName + "==>ID:" + SelectID);
				ed.setText(selectName);
				list.get(p).setType(selectName);
			}
		});
		relative_all.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
	}
}
